﻿using System;
using System.Collections.Generic;
using System.Threading;
using CosyTrade.ClientForm.Printer;
using CosyTrade.Financial;
using CosyTrade.Organization;
using CosyTrade.Trade;
using CosyTrade.UI;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using NHibernate.Expression;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Trade
{
    public partial class ExportList_Finish : FormBase
    {
        public ExportList_Finish()
        {
            InitializeComponent();
        }

        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem("ExportBillDetail", Resources.table, "出口单明细", true, true, true,BillDetailCallback);

            ToolbarItems.AddCommandItem("PrintExportBill", Resources.printer, "打印出口单", true, true, true, PrintExportCallback);

            ToolbarItems.AddCommandItem("RefreshExportBill", Resources.reload, "刷新列表", true, true, true,RefreshCallback);

            ToolbarItems.AddCommandItem("Close", Resources.door_out, "关闭", true, true, true,CloseCallback);
        }

        private void CloseCallback(object sender, EventArgs e)
        {
            Close();
        }

        private void RefreshCallback(object sender, EventArgs e)
        {
            ((ThreadStart)LoadDataInvoke).BeginInvoke(null, null);            
        }

        private void BillDetailCallback(object sender, EventArgs e)
        {
            OpenCurrentDetail();
        }

        private void PrintExportCallback(object sender, EventArgs e)
        {
            ExportBillBase exportBill = bindingSource1.Current as ExportBillBase;
            if (exportBill == null)
            {
                Functions.ShowMessageBox("请选择要打印的出口提单");
                return;
            }
            PrintExport printExportBill = new PrintExport(exportBill);
            printExportBill.ShowDialog();
        }

        private void OpenCurrentDetail()
        {
            ExportBillBase bill = GetCurrentObj();
            if (bill == null)
            {
                Functions.ShowConfigBox("请选择要查看的出口单");
                return;
            }
            ExportDetailForm form = new ExportDetailForm(bill.PrimaryKey);
            form.ReadOnlyMode = true;
            form.Show();
        }

        ExportBillBase GetCurrentObj()
        {
            ExportBillBase bill = bindingSource1.Current as ExportBillBase;
            return bill;
        }

        private void rptCompany_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            Company cmp = e.Value as Company;
            if (cmp != null) e.DisplayText = cmp.Caption;
        }

        private void rptEmployee_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            Employee emp = e.Value as Employee;
            if (emp != null) e.DisplayText = emp.EmpName;
        }

        private void rptRate_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            ExchangeRate rate = e.Value as ExchangeRate;
            if (rate != null) e.DisplayText = rate.Exchange.ToString();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ((ThreadStart)LoadDataInvoke).BeginInvoke(null, null);
        }

        private void ExportList_Finish_Load(object sender, EventArgs e)
        {
            colTotal.Visible = colTotal2.Visible = GlobalInfo.CurrentRole.Powerset.Trade_ViewExportBillPrice;

            IList<Company> cmpLst = Operation.OrganizationSystem.GetCompanyList();
            foreach (Company company in cmpLst)
            {
                if (company.CompanyType != CompanyType.Trade) continue;
                ImageComboBoxItem item = new ImageComboBoxItem(company.Caption, company);
                cbCompany.Properties.Items.Add(item);
                if (cbCompany.SelectedItem == null) cbCompany.SelectedItem = item;
            }

            cbSearchYear.Properties.Items.Clear();
            for (int i = 2007; i <= 2020; i++)
            {
                ImageComboBoxItem item = new ImageComboBoxItem(i.ToString(), i);
                cbSearchYear.Properties.Items.Add(item);
                if (i == DateTime.Now.Year) cbSearchYear.SelectedItem = item;
            }
            cbSearchMonth.Properties.Items.Clear();
            for (int i = 1; i <= 12; i++)
            {
                ImageComboBoxItem item = new ImageComboBoxItem(i.ToString(), i);
                cbSearchMonth.Properties.Items.Add(item);
                if(i==DateTime.Now.Month) cbSearchMonth.SelectedItem = item;
            }
            ((ThreadStart)LoadDataInvoke).BeginInvoke(null, null);
        }

        private void LoadDataInvoke()
        {
            DateTime startDate = new DateTime(Convert.ToInt32(cbSearchYear.EditValue), Convert.ToInt32(cbSearchMonth.EditValue), 1);
            DateTime endDate = startDate.AddMonths(1).AddDays(-1);

            IList<ExportBillBase> dataSource = Operation.TradeSystem.GetFinishExportBillByExporter(startDate, endDate, FinishExportBillType.Receive);
            bindingSource1.DataSource = dataSource;
        }

        private void gridView1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            e.SuppressKeyPress = false;
            if (e.KeyCode == System.Windows.Forms.Keys.Enter) OpenCurrentDetail();
        }

        private void gridView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks < 2) return;
            GridHitInfo hitInfo = gridView1.CalcHitInfo(e.Location);
            if(hitInfo.InRow) OpenCurrentDetail();
        }

        private void btnSearchByCompany_Click(object sender, EventArgs e)
        {
            Company selectCmp = cbCompany.EditValue as Company;
            if(selectCmp==null)
            {
                Functions.ShowMessageBox("请选择要查询的公司");
                return;
            }

            using(new CursorHelper(this))
            {
                ICriterion criterion = Expression.And(Expression.Eq("ToCompany.PrimaryKey", selectCmp.PrimaryKey),
                                                      Expression.Eq("BillStat", ExportBillStat.Finish));
                IList<ExportBillBase> dataSource = Operation.TradeSystem.SearchExportBill(criterion, 1000, Order.Desc("FinishDate"));
                bindingSource1.DataSource = dataSource;
            }
        }

        private void btnSearchByBill_Click(object sender, EventArgs e)
        {
            if (Functions.ValueIsEmpty(txtBillNumber, "请输入提单号")) return;
            using (new CursorHelper(this))
            {
                ICriterion criterion = Expression.And(Expression.Eq("TradeCode", txtBillNumber.Text),
                                                      Expression.Eq("BillStat", ExportBillStat.Finish));
                IList<ExportBillBase> dataSource = Operation.TradeSystem.SearchExportBill(criterion, 1000, Order.Desc("FinishDate"));
                bindingSource1.DataSource = dataSource;
            }
        }

        private void btnSearchByCN_Click(object sender, EventArgs e)
        {
            if (Functions.ValueIsEmpty(txtCN, "请输入货柜号")) return;
            using (new CursorHelper(this))
            {
                ICriterion criterion = Expression.And(Expression.Eq("ContainerNumber", txtCN.Text),
                                                      Expression.Eq("BillStat", ExportBillStat.Finish));
                IList<ExportBillBase> dataSource = Operation.TradeSystem.SearchExportBill(criterion, 1000, Order.Desc("FinishDate"));
                bindingSource1.DataSource = dataSource;
            }
        }
    }
}
